package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Usuario;

public class JDBCUsuarioDAO implements IUsuarioDAO{
	
	private Connection conn;
	
	public JDBCUsuarioDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
	 protected void finalize() {
	        ConnectionManager.getInstance().checkIn(conn);
	    }

	
	public boolean Logado(String id_nick, String id_password) {
		boolean respuesta = false;
		
		String sql = "SELECT * FROM usuario WHERE (nick = ? AND password = ?) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
 
        try {

            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, id_nick);
            stmt.setString(2, id_password);
            result = stmt.executeQuery();
           
            if(result.next())
            	respuesta =true;
  
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
		
        return respuesta;
	}

	public void insertUsuario(Usuario us,String oid) {
		String sql = "INSERT INTO usuario (nick, password,OID) VALUES (?, ?,?)";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, us.getNick());
            stmt.setString(2, us.getPassword());
            
            stmt.setString(3, oid);

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }		
	}

	
	public void deleteUsuario(Usuario us) {
		String sql = "DELETE FROM usuario WHERE (nick = ? AND password = ?)";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, us.getNick());
            stmt.setString(2, us.getPassword());

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }		
		
	}

	public void updateUsuario(String nick,String password,String oid) {
		String sql = "UPDATE usuario SET nick = ? , password = ? WHERE oid = ?";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1,nick);
			stmt.setString(2,password);
			stmt.setString(3,oid);
			
			stmt.executeUpdate();
			}
		catch (SQLException e) {
			
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
	}
	
	public String obtenerOID(String nick) {
		String res = "";
		String sql = "SELECT OID FROM usuario WHERE Nick =?";
		PreparedStatement stmt = null;
		ResultSet result = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, nick);

			result = stmt.executeQuery();

			if (result.next()) {
				res = result.getString(1);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());

		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
		return res;
	}

	
	public Boolean existeNick(String nick) {
		boolean b = false;
		
		String sql = "SELECT * FROM usuario WHERE nick = ?";
		PreparedStatement stmt = null;
		ResultSet result = null;
		
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, nick);
			result = stmt.executeQuery();

			if (result.next())
				b = true;

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}if (result != null)
					result.close();
			} catch (SQLException e) {
			}
		}
		return b;
	}

}
